package com.cdut.hwadeefsc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cdut.hwadeefsc.entity.users;
import com.cdut.hwadeefsc.mapper.usersMapper;
import com.cdut.hwadeefsc.service.usersService;
import jakarta.annotation.Resource;
import org.mindrot.jbcrypt.BCrypt;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;

import java.util.Date;
import java.util.List;

@Service
public class usersServiceImpl extends ServiceImpl<usersMapper, users> implements usersService {

    @Resource
    private usersMapper usersMapper;

    @Override
    public String getCurrentId() {
        // 直接从请求属性中获取用户ID头信息，不依赖HttpServletRequest
        RequestAttributes attributes = RequestContextHolder.getRequestAttributes();
        if (attributes == null) {
            return null;
        }
        return attributes.getAttribute("X-User-Id", RequestAttributes.SCOPE_REQUEST) != null
                ? attributes.getAttribute("X-User-Id", RequestAttributes.SCOPE_REQUEST).toString()
                : null;
    }

    @Override
    public boolean registerParent(users parent) {
        parent.setRole("parent");
        return save(parent);
    }

    @Override
    public List<users> getUserList(String role, Long classId, int page, int size) {
        QueryWrapper<users> queryWrapper = new QueryWrapper<>();
        if (role != null) {
            queryWrapper.eq("role", role);
        }
        return baseMapper.selectPage(new Page<>(page, size), queryWrapper).getRecords();
    }

    @Override
    public boolean approveParent(Long Id) {
        users user = getById(Id);
        if (user == null || !"parent".equals(user.getRole())) {
            return false;
        }
        user.setUpdatedAt(new Date());
        return updateById(user);
    }

    @Override
    public boolean createUser(users user) {
        if (!"teacher".equals(user.getRole()) && !"leader".equals(user.getRole())) {
            return false;
        }
        return save(user);
    }

    @Override
    public IPage<users> queryUserPage(Page<users> page, String role, Long organizationId) {
        QueryWrapper<users> queryWrapper = new QueryWrapper<>();
        if (role != null) {
            queryWrapper.eq("role", role);
        }
        if (organizationId != null) {
            queryWrapper.eq("organization_id", organizationId);
        }
        return baseMapper.selectPage(page, queryWrapper);
    }

    @Override
    public IPage<users> getUnapprovedUsers(Page<users> page) {
        QueryWrapper<users> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("role", "parent")
                .ge("created_at", new Date(System.currentTimeMillis() - 30L * 24 * 60 * 60 * 1000));
        return baseMapper.selectPage(page, queryWrapper);
    }

    @Override
    public String getHeader(String headerName) {
        // 直接从请求属性中获取指定头信息，不依赖HttpServletRequest
        RequestAttributes attributes = RequestContextHolder.getRequestAttributes();
        if (attributes == null) {
            return null;
        }
        Object headerValue = attributes.getAttribute(headerName, RequestAttributes.SCOPE_REQUEST);
        return headerValue != null ? headerValue.toString() : null;
    }

    @Override
    public boolean checkNameExists(String name) {
        QueryWrapper<users> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name", name);
        return baseMapper.exists(queryWrapper);
    }

    @Override
    public users login(String name, String password) {
        QueryWrapper<users> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name", name);
        users user = baseMapper.selectOne(queryWrapper);

        //if (user != null && BCrypt.checkpw(password, user.getPassword())) {
        //    return user;
       // }
        return null;
    }
}